package com.example.qimo

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.ListView
import androidx.appcompat.widget.Toolbar
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.example.qimo.FavoriteCitiesActivity
import com.example.qimo.ForecastAdapter
import com.example.qimo.MainActivity
import com.example.qimo.MemoActivity
import com.example.qimo.R
import com.example.qimo.weather.Forecast
import com.example.qimo.weather.WeatherResponse
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class BeijingWeatherActivity : AppCompatActivity() {

    private lateinit var listView: ListView
    private lateinit var queue: RequestQueue
    private val baseURL = "http://t.weather.itboy.net/api/weather/city/"
    private lateinit var toolbar: Toolbar

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTheme(R.style.AppTheme) // 设置默认主题
        setContentView(R.layout.activity_beijing_weather)

        toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)

        listView = findViewById(R.id.listView)
        queue = Volley.newRequestQueue(this)

        loadBeijingWeather()
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.action_open_second_activity -> {
                openSecondActivity()
                true
            }
            R.id.action_open_memo -> {
                openMemoActivity()
                true
            }
            R.id.action_open_favorite_cities -> {
                openFavoriteCitiesActivity()
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }

    private fun openSecondActivity() {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
    }

    private fun openMemoActivity() {
        val intent = Intent(this, MemoActivity::class.java)
        startActivity(intent)
    }

    private fun openFavoriteCitiesActivity() {
        val intent = Intent(this, FavoriteCitiesActivity::class.java)
        startActivity(intent)
    }

    private fun loadBeijingWeather() {
        val stringRequest = StringRequest(
            Request.Method.GET,
            baseURL + "101010100", // 北京的 city_code
            { response ->
                val gson = Gson()
                val WeatherType: java.lang.reflect.Type = object : TypeToken<WeatherResponse>() {}.type
                val weather: WeatherResponse = gson.fromJson(response, WeatherType)
                setupListView(weather.data.forecast)
            },
            { error ->
                Log.e("BeijingWeather", "Error: ${error.message}")
            }
        )
        queue.add(stringRequest)
    }

    private fun setupListView(forecastList: List<Forecast>) {
        val adapter = ForecastAdapter(this, forecastList)
        listView.adapter = adapter
    }
}